<template>
  <div id="nav">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>导航管理</span>
      </div>
      <el-row :gutter="5">
        <el-col :span="1.5">
          <el-button @click="dialogVisible = true" plain type="primary" size="small" icon="el-icon-search">增加</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button @click="open(...checkboxall)" :disabled="checkboxall.length != 1" plain type="success" size="small"
            icon="el-icon-search">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button @click="delNav()" :disabled="checkboxall.length == 0" plain type="danger" size="small"
            icon="el-icon-search">删除</el-button>
        </el-col>
      </el-row>
      <el-table @selection-change="handleSelectionChange" height="80%" :data="tableData"
        style="width: 100%;margin: 30px 0;">
        <el-table-column align="center" type="selection">
        </el-table-column>
        <el-table-column align="center" prop="icon" label="网址图标">
          <template slot-scope="scope">
            <img v-show="scope.row.icon" style="width: 100px;height: auto; object-fit: cover;" :src="scope.row.icon">
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip align="center" prop="nav_name" label="网址名称">
        </el-table-column>
        <el-table-column show-overflow-tooltip align="center" width="200px" prop="website" label="网站地址">
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-switch :active-value="0" :inactive-value="1" @change="switch_value(scope.row)"
              v-model="scope.row.is_delete">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column :width="200" align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click="open(scope.row)" size="small" type="primary" icon="el-icon-edit">修改</el-button>
            <el-button @click="delNav(scope.row.nav_id)" size="small" type="danger" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination style="text-align: center; margin-top: 20px;" @size-change="sizeChange"
        @current-change="currentChange" :current-page="pagination.pagenum" :page-sizes="pagination.pageSizes"
        :page-size="pagination.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total">
      </el-pagination>

      <!-- 增加 修改 -->
      <!-- 增加 -->
      <el-dialog :close-on-click-modal="false" width="400px" :title="isAdd ? '添加' : '修改'" :visible.sync="dialogVisible"
        @close="closedialog">
        <el-form :rules="rules" style="width: 100%;" ref="dialog_form" :model="dialog_form" label-width="80px">
          <el-form-item prop="nav_name" label="网站名称">
            <el-input v-model="dialog_form.nav_name" size="small" placeholder="网站名称"></el-input>
          </el-form-item>
          <el-form-item prop="website" label="网站地址">
            <el-input v-model="dialog_form.website" size="small" placeholder="网站地址"></el-input>
          </el-form-item>
          <el-form-item label="网站图标">
            <el-upload class="avatar-uploader" action="#" :http-request="httpRequest" :show-file-list="false">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-if="imageUrl" class="x el-icon-circle-close" @click.stop="closeIcon"></i>
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitdialog">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getNav, setNav, addNav, delNav } from "@/api/index";
export default {
  name: 'classification',
  data() {
    return {
      tableData: [],
      pagination: {
        type: 1,
        pageSizes: [2, 4, 6, 8, 10],
        pagesize: 2,
        pagenum: 1,
        total: 0,
      },
      checkboxall: [],
      isAdd: true,
      dialogVisible: false,
      dialog_form: {
        nav_name: '',
        website: '',
        icon: '',
      },
      rules: {
        nav_name: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
        website: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
      },
      imageUrl: '',
    }
  },
  mounted() {
    this.getNav();
  },
  methods: {
    //获取分类
    getNav() {
      getNav(this.pagination).then(res => {
        if (res.data.status) {
          this.tableData = res.data.results
          this.pagination.total = res.data.total
        } else {
          this.$message.error('错误')
        }
      })
    },
    //开关回调
    switch_value(row) {
      //通过id修改状态
      let data = {
        nav_id: row.nav_id,
        is_delete: row.is_delete
      }
      setNav(data).then((res) => {
        if (!res.data.status) {
          this.$message.error(res.data.message);
          row.is_delete = !row.is_delete
        }
      })
    },
    //选中回调
    handleSelectionChange(val) {
      this.checkboxall = val;
    },
    //修改pagesize
    sizeChange(e) {
      this.pagination.pagesize = e;
      this.getNav();
    },
    //修改pagenum
    currentChange(e) {
      this.pagination.pagenum = e;
      this.getNav();
    },
    //关闭弹窗回调
    closedialog() {
      this.isAdd = true;
      this.imageUrl = '';
      this.dialog_form = {
        nav_name: '',
        website: '',
        icon: '',
        icon: '',
      }
      this.$refs.dialog_form.resetFields();
      this.$refs.dialog_form.clearValidate();
    },
    //上传图片
    httpRequest(e) {
      let res = !e.file ? e.target.files[0] : e.file; //兼容原生和element 上传
      let reader = new FileReader()
      reader.readAsDataURL(res)
      reader.onload = () => {//加载完立即触发
        this.dialog_form.icon = this.imageUrl = reader.result; // base64字符串
      }
    },
    //点击修改
    open(row) {
      this.isAdd = false;
      this.dialogVisible = true
      this.dialog_form = row;
      this.imageUrl = row.icon
    },
    //弹框确定
    submitdialog() {
      this.$refs.dialog_form.validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            //增加模式
            addNav(this.dialog_form).then((res) => {
              if (res.data.status) {
                this.getNav();
                this.$message.success('成功')
                this.dialogVisible = false;
              } else {
                this.$message.error('错误')
              }
            })
          } else {
            //修改模式
            setNav(this.dialog_form).then((res) => {
              if (res.data.status) {
                this.getNav();
                this.$message.success('成功')
                this.dialogVisible = false;
              } else {
                this.$message.error('错误')
              }
            })
          }
        }
      })
    },
    //删除/多选删除
    delNav(id) {
      delNav({
        list: id ? [id] : this.checkboxall.map((item) => {
          return item.nav_id
        })
      }).then(res => {
        if (res.data.status) {
          this.dialogVisible = false;
          this.pagination.pagenum = 1;
          this.getNav();
          this.$message.success('成功')
        } else {
          this.$message.error('错误')
        }
      })
    },
    closeIcon() {
      this.imageUrl = ''
      this.dialog_form.icon = null
    }
  }
}
</script>

<style lang="less" src="./index.less" scoped></style>